<?php


namespace App\frontend\services;

use App\common\services\BaseServices;
use App\frontend\models\DetailsBalanceModel;
use App\frontend\models\DetailPointsModel;
use App\frontend\models\DetailsPocketMoneyModel;

class BusinessDetailLogServices extends BaseServices
{

    const AddOperate = '+';
    const subtractOperate = '-';

    /**
     * 保存日志明细入口
     * @param $type 类型 1积分  2余额  3零钱
     * @param $buss_type 业务类型
     * @param $num 金额/积分
     * @param $data 其他数据
     * @return mixed
     */
    public static function SaveDetailsLog($type,$buss_type, $num, $data = []){
        switch ($type){
            case 1:
                $action= 'pointsDetailLog';
                break;
            case 2:
                $action= 'balanceDetailLog';
                break;
            case 3:
                $action= 'pocketMoneyDetail';
                break;
        }
        return self::$action($buss_type, $num, $data);

    }
    /**
     * 积分明细保存
     * @param $buss_type 业务类型
     * @param $points 积分
     * @param $data  其他需要的数据
     * @return array
     */
    protected function pointsDetailLog($buss_type, $points, $data = [])
    {
        if (!$buss_type || empty($points) || empty($data['uid'])) {
            return self::returnError(40101, 'point_log参数错误!');

        }
        $buss_name = '';
        $operate = '';
        $buss_desc = '';
        switch ($buss_type) {
            case 'sign_in':
                $buss_name = '签到';
                $operate = self::AddOperate;
                $buss_desc = $buss_name . $operate . $points . '积分';
                break;
            case 'add_point_lottery':
                $buss_name = '积分抽签-获得';
                $operate = self::AddOperate;
                $buss_desc = $buss_name . $operate . $points . '积分';
                break;
            case 'sub_point_lottery':
                $buss_name = '积分抽奖-扣除';
                $operate = self::subtractOperate;
                $buss_desc = $buss_name . $operate . $points . '积分';
                break;
            case 'shopping_send':
                $buss_name = '购物送积分-' . $data['goods_name'];
                $operate = self::AddOperate;
                $buss_desc = $buss_name . $operate . $points . '积分';
                break;
            default:
                return self::returnError(40102, '类型错误!');
                break;
        }
        $log_data = [];
        $log_data['uid'] = $data['uid'];
        $log_data['buss_type'] = $buss_type;
        $log_data['buss_name'] = $buss_name;
        $log_data['operate'] = $operate;
        $log_data['points'] = $points;
        $log_data['buss_desc'] = $buss_desc;
        $log_data['create_time'] = time();
        //保存数据
        $model = DetailPointsModel::getInstance();
        $res = $model->add($log_data);
        if (!$res) {
            return self::returnError(40103, '新增失败!');
        }
        return self::returnSuccess('新增成功!');
    }

    /**
     * 余额明细保存
     * @param $buss_type 业务类型
     * @param $money  金额
     * @param $data 其他数据
     * @return array
     */
    protected function balanceDetailLog($buss_type, $money, $data = [])
    {
        if (!$buss_type || !$money || empty($data['uid'])) {
            return self::returnError(40113, 'point_log参数错误!');
        }
        $buss_name = '';
        $operate = '';
        $buss_desc = '';
        switch ($buss_type) {
            case 'recharge':
                $buss_name = '充值  ';
                $operate = self::AddOperate;
                $buss_desc = $buss_name  . $operate . $money;
                break;
            case 'shopping':
                $buss_name = '商城购物  ';
                $operate = self::subtractOperate;
                $buss_desc = $buss_name . $operate . $money;
                break;
            default:
                return self::returnError(40112, '类型错误!');
                break;
        }
        $log_data = [];
        $log_data['uid'] = $data['uid'];
        $log_data['buss_type'] = $buss_type;
        $log_data['buss_name'] = $buss_name;
        $log_data['operate'] = $operate;
        $log_data['money'] = $money;
        $log_data['buss_desc'] = $buss_desc;
        $log_data['create_time'] = time();
        //保存数据
        $model = DetailsBalanceModel::getInstance();
        $res = $model->add($log_data);
        if (!$res) {
            return self::returnError(40113, '新增失败!');
        }
        return self::returnSuccess('新增成功!');
    }

    /**
     * 零钱明细保存
     * @param $buss_type 业务类型
     * @param $money 金额
     * @param $data 其他数据
     * @return array
     */
    protected function pocketMoneyDetail($buss_type, $money, $data = [])
    {
        if (!$buss_type || !$money || empty($data['uid'])) {
            return self::returnError(40121, 'point_log参数错误!');

        }
        $buss_name = '';
        $operate = '';
        $buss_desc = '';
        switch ($buss_type) {
            case 'commission':
                $buss_name = '佣金';
                $operate = self::AddOperate;
                $buss_desc = $buss_name . $operate . $money;
                break;
            case 'withdrawal':
                $buss_name = '提现';
                $operate = self::subtractOperate;
                $buss_desc = $buss_name . $operate . $money;
                break;
            default:
                return self::returnError(40122, '类型错误!');
                break;
        }
        $log_data = [];
        $log_data['uid'] = $data['uid'];
        $log_data['buss_type'] = $buss_type;
        $log_data['buss_name'] = $buss_name;
        $log_data['operate'] = $operate;
        $log_data['money'] = $money;
        $log_data['buss_desc'] = $buss_desc;
        $log_data['create_time'] = time();
        //保存数据
        $model = DetailsPocketMoneyModel::getInstance();
        $res = $model->add($log_data);
        if (!$res) {
            return self::returnError(40123, '新增失败!');
        }
        return self::returnSuccess('新增成功!');
    }


}
